$(function () {
    if (WebUploader) {
        $("div[uploaderOption]").each(function () {
            var $this = $(this);

            var uploaderOption = DWZ.jsonEval($this.attr("uploaderOption"));
            console.log("11111");
            var $list = $('#' + uploaderOption.fileList), $btn = $('#' + uploaderOption.fileBnt),
                $divId = $this.attr("id");
            var uploader = WebUploader.create({
                swf: hostUrl + '/js/Uploader.swf',
                server: uploaderOption.server,
                pick: "#" + $divId,
                resize: false,
                fileVal: uploaderOption.fileVal,
                fileSingleSizeLimit: uploaderOption.fileSize || 10 * 1024 * 1024,
                accept: uploaderOption.accept
            });
            uploader.on('fileQueued', function (file) {
                $list.append('<div id="' + file.id + '" class="item">' +
                    '<h4 class="info">' + file.name + '</h4>' +
                    '</div>');
            });
            uploader.on('uploadProgress', function (file, percentage) {
                var $li = $('#' + file.id),
                    $percent = $li.find('.progress .progress-bar');

                if (!$percent.length) {
                    $percent = $('<div class="progress progress-striped active">' +
                        '<div class="progress-bar" role="progressbar" style="width: 0%">' +
                        '</div>' +
                        '</div>').appendTo($li).find('.progress-bar');
                }
                $li.find('p.state').text('上传中');
                $percent.css('width', percentage * 100 + '%');
            });

            uploader.on('beforeFileQueued', function (file) {
                if (file) {
                    var name = file.name;
                    if (name) {
                        var extend = name.substring(name.lastIndexOf(".") + 1);
                        if (extend != "bat"
                            && extend != "exe" && extend != "sh") {
                            return true;
                        } else {
                            alertMsg.warn(uploaderOption.beforeFileQueuedMsg);
                        }
                    }
                }
                return false;
            });

            uploader.on('uploadSuccess', function (file, response) {
                $list.empty();
                if (response && response.data) {
                    uploaderOption.uploadSuccess.call(this, response.data);
                }
            });

            $btn.on('click', function () {
                uploader.upload();
            });
        });
    }
})